package com.jeeplus.modules.tms.util;

import com.jeeplus.modules.tms.entity.TmsWaybill;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.apache.pdfbox.multipdf.PDFMergerUtility;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by lichaohui on 2018/4/19.
 */
public class Print {

    public void mergePdf(String folder, String outPdfFile, List<String> fileString) throws Exception {
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        List<String> filesInFolder = fileString;
        for (int i = 0; i < filesInFolder.size(); i++)
            mergePdf.addSource(folder + File.separator + filesInFolder.get(i));
        mergePdf.setDestinationFileName(outPdfFile);
        mergePdf.mergeDocuments();
    }

    public void updatePdfa1(TmsWaybill tmsWaybill, String templateFile, File file) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(templateFile); // 模版文件目录
        FileOutputStream fos = new FileOutputStream(file);

        PdfStamper ps = new PdfStamper(reader, fos); // 生成的输出流

        BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

        AcroFields s = ps.getAcroFields();

        //主表
        s.setFieldProperty("Text1", "textfont", bf, null);
        s.setFieldProperty("Text2", "textfont", bf, null);
        s.setFieldProperty("Text3", "textfont", bf, null);
        s.setFieldProperty("Text4", "textfont", bf, null);
        s.setFieldProperty("Text5", "textfont", bf, null);
        s.setFieldProperty("Text6", "textfont", bf, null);
        s.setFieldProperty("Text7", "textfont", bf, null);
        s.setFieldProperty("Text8", "textfont", bf, null);
        s.setFieldProperty("Text9", "textfont", bf, null);
        s.setFieldProperty("Text10", "textfont", bf, null);
        s.setFieldProperty("Text11", "textfont", bf, null);
        s.setFieldProperty("Text12", "textfont", bf, null);

        s.setField("Text1", tmsWaybill.getForwardingPrice());
        s.setField("Text2", tmsWaybill.getDriverName());
        s.setField("Text3", tmsWaybill.getPlateNumber());
        s.setField("Text4", tmsWaybill.getRentingCost());
        s.setField("Text5", "123456111111111111111111111111111111111111111111");
        s.setField("Text6", "123456111111111111111111111111111111111111111111");
        s.setField("Text7", "123456111111111111111111111111111111111111111111");
        s.setField("Text8", "123456111111111111111111111111111111111111111111");
        s.setField("Text9", "123456111111111111111111111111111111111111111111");
        s.setField("Text10", "123456111111111111111111111111111111111111111111");
        s.setField("Text11", "123456111111111111111111111111111111111111111111");
        s.setField("Text12", tmsWaybill.getSite());
        ps.setFormFlattening(true); // 这句不能少

        ps.close();
        reader.close();
        fos.close();
    }
}
